package com.lottery.im.module.tim.model;

import android.content.Context;
import android.text.TextUtils;
import com.lottery.im.MyApplication;
import com.lottery.im.R;
import com.lottery.im.constant.Constants;
import com.lottery.im.constant.RxBusCode;
import com.lottery.im.module.chat.ui.ChatActivity;
import com.lottery.im.module.tim.event.RefreshEvent;
import com.lottery.im.module.tim.message.GroupTipMessage;
import com.lottery.im.module.tim.message.Message;
import com.lottery.im.module.tim.message.TextMessage;
import com.lottery.im.util.SPUtils;
import com.lottery.im.util.rxbus.RxBus;
import com.tencent.imsdk.TIMConversation;
import com.tencent.imsdk.TIMConversationType;
import com.tencent.imsdk.ext.message.TIMConversationExt;
import com.tencent.imsdk.ext.message.TIMManagerExt;

/**
 * 好友或群聊的会话
 */
public class NomalConversation extends Conversation {

  private TIMConversation conversation;

  //最后一条消息
  private Message lastMessage;

  public NomalConversation(TIMConversation conversation) {
    this.conversation = conversation;
    type = conversation.getType();
    identify = conversation.getPeer();
  }

  public void setLastMessage(Message lastMessage) {
    this.lastMessage = lastMessage;
  }

  @Override
  public int getAvatar() {
    return R.drawable.icon_conversation_de;
  }

  @Override public String getAvatarUrl() {
    switch (type) {
      case C2C:
        FriendProfile profile = FriendshipInfo.getInstance().getProfile(identify);
        if (null != profile) {
          avatar = profile.getAvatarUrl();
        }
        return avatar;
      case Group:
        return GroupInfo.getInstance().getGroupAvatarUrl(identify);
    }
    return "";
  }

  /**
   * 跳转到聊天界面或会话详情
   *
   * @param context 跳转上下文
   */
  @Override
  public void navToDetail(Context context) {

    if (!identify.equals(Constants.SYSTEM_IDENTIFY)) {
      ChatActivity.navToChat(context, identify, type);
    }else {
      RxBus.get().send(RxBusCode.SYSTEM_PUSH_CODE);
      deleteConversationAndLocalMsgs();
    }
  }

  /**
   * 获取最后一条消息摘要
   */
  @Override
  public String getLastMessageSummary() {
    TIMConversationExt ext = new TIMConversationExt(conversation);
    if (ext.hasDraft()) {
      TextMessage textMessage = new TextMessage(ext.getDraft());
      if (lastMessage == null || lastMessage.getMessage().timestamp() < ext.getDraft()
          .getTimestamp()) {
        return MyApplication.getInstance().getString(R.string.conversation_draft)
            + textMessage.getSummary();
      } else {

        if (lastMessage.getSummary().equals(GroupTipMessage.NO_TIP_TEXT)){
          return "";
        }

        return lastMessage.getSummary();
      }
    } else {
      if (lastMessage == null) return "";
      if (lastMessage.getSummary().equals(GroupTipMessage.NO_TIP_TEXT)){
        return "";
      } return lastMessage.getSummary();
    }
  }

  /**
   * 获取名称
   */
  @Override
  public String getName() {
    if (type == TIMConversationType.Group) {
      name = GroupInfo.getInstance().getGroupName(identify);
      if (name.equals("")) name = identify;
    } else {
      FriendProfile profile = FriendshipInfo.getInstance().getProfile(identify);
      if (null != profile) name = profile.getName();
      if (TextUtils.isEmpty(name)) name = identify;
    }
    return name;
  }

  @Override public void deleteConversationAndLocalMsgs() {
    if (TIMManagerExt.getInstance().deleteConversationAndLocalMsgs(type, identify)) {
      RefreshEvent.getInstance().onRefresh();
    }
  }

  /**
   * 获取未读消息数量
   */
  @Override
  public long getUnreadNum() {
    if (conversation == null) return 0;
    TIMConversationExt ext = new TIMConversationExt(conversation);
    return ext.getUnreadMessageNum();
  }

  /**
   * 将所有消息标记为已读
   */
  @Override
  public void readAllMessage() {
    if (conversation != null) {
      TIMConversationExt ext = new TIMConversationExt(conversation);
      ext.setReadMessage(null, null);
    }
  }

  /**
   * 获取最后一条消息的时间
   */
  @Override
  public long getLastMessageTime() {
    TIMConversationExt ext = new TIMConversationExt(conversation);
    if (ext.hasDraft()) {
      if (lastMessage == null || lastMessage.getMessage().timestamp() < ext.getDraft()
          .getTimestamp()) {
        return ext.getDraft().getTimestamp();
      } else {
        return lastMessage.getMessage().timestamp();
      }
    }
    if (lastMessage == null) {
      long time = (long) SPUtils.get(MyApplication.getInstance(), identify, 0l);
      return time;
    }
    return lastMessage.getMessage().timestamp();
  }

  /**
   * 获取会话类型
   */
  public TIMConversationType getType() {
    return conversation.getType();
  }
}
